package com.ims.controller.system.course;


import com.ims.controller.base.BaseController;
import com.ims.entity.Page;
import com.ims.entity.system.Course;
import com.ims.entity.system.Menu;
import com.ims.service.CourseMapper;
import com.ims.shiro.ShiroSessionService;
import com.ims.util.Jurisdiction;
import com.ims.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * 类名称：CourseController
 * 创建人：nsp
 * 创建时间：2018年2月6日
 * @version*/


@Controller
@RequestMapping(value="/course")
public class CourseController extends BaseController {

    //String studentScoreUrl = "student/listStudentScore.shtml"; //菜单地址(权限用)

    @Resource
    ShiroSessionService session;

    @Autowired
    private CourseMapper courseMapper;

    @RequestMapping(value="/listCourse")
    public ModelAndView listCourse(HttpServletRequest request, HttpServletResponse response, Page page)throws Exception{
        String path = request.getServletPath();
        path = path.substring(1, path.length());
        ModelAndView mv = this.getModelAndView();
        boolean b = Jurisdiction.hasJurisdiction(path,session);
        if(b)
        {
            PageData pd = new PageData();
            pd = this.getPageData();
            page.setPd(pd);

            List<PageData> list=courseMapper.listAllCourse(page);

            mv.addObject("courseList",list);
            mv.setViewName("system/course/course_list");

            return mv;
        }
        else
        {
            mv.setViewName("redirect:/logout.shtml");
            return mv;
        }

    }


    @RequestMapping(value="/toAdd")
    public ModelAndView toAdd()throws Exception{
        ModelAndView mv = this.getModelAndView();
        try{
            //mv.addObject("menuList", menuList);
            mv.setViewName("system/course/course_add");
        } catch(Exception e){
            logger.error(e.toString(), e);
        }
        return mv;
    }


    @RequestMapping(value="/add")
    public ModelAndView add(Course course)throws Exception{
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        try{

            System.out.println(course.getCourseId());
            System.out.println(course.getCourseName());
            System.out.println(course.getUserId());

            courseMapper.insert(course);
            mv.addObject("msg","success");
        } catch(Exception e){
            logger.error(e.toString(), e);
            mv.addObject("msg","failed");
        }
        mv.setViewName("save_result");
        return mv;

    }


    @RequestMapping(value="/toEdit")
    public ModelAndView toEdit(Integer cId)throws Exception{
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();
        try{

            //mv.addObject("student",pd);

            System.out.println(cId);
            Course res=courseMapper.selectByPrimaryKey(cId);
            System.out.println(res.getcId()+"---");
            System.out.println(res.getCourseId()+"---");
            System.out.println(res.getCourseName()+"---");
            System.out.println(res.getUserId()+"---");

            mv.addObject("oldCourse",res);
            mv.setViewName("system/course/course_edit");
        } catch(Exception e){
            logger.error(e.toString(), e);
        }
        return mv;
    }

    @RequestMapping(value="/edit")
    public ModelAndView edit(Course course)throws Exception{
        ModelAndView mv = this.getModelAndView();

        try{

            courseMapper.updateByPrimaryKey(course);


            mv.addObject("msg","success");
        } catch(Exception e){
            logger.error(e.toString(), e);
            mv.addObject("msg","failed");
        }
        mv.setViewName("save_result");
        return mv;
    }


    @RequestMapping(value="/del")
    public void delete(@RequestParam Integer cId, PrintWriter out)throws Exception{

        try{
           courseMapper.deleteByPrimaryKey(cId);
            out.write("success");
            out.flush();
            out.close();
        } catch(Exception e){
            logger.error(e.toString(), e);
        }

    }

}
